﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;

namespace commonV2.controls
{
    public partial class baseReportViewer : CrystalDecisions.Windows.Forms.CrystalReportViewer
    {
        public delegate void onPrinted(object sender);
        public event onPrinted myOnPrinted=null;
        public baseReportViewer()
        {
            InitializeComponent();
            this.ShowParameterPanelButton = false;
            this.ShowRefreshButton = false;
        }

        private ColorTypes _colorType = ColorTypes.Normal;
        public ColorTypes myColorType
        {
            set
            {
                _colorType = value;
                ApplyTheme();
            }
            get
            {
                return _colorType;
            }
        }
        protected virtual void ApplyTheme()
        {
            if (Settings.sysIsDesignMode) return;
            ColorSet cs = ThemeLibs.GetColorSet(this.myColorType);
            if (cs != null)
            {
                this.BackColor = cs.BackColor;
                this.ForeColor = cs.ForeColor;
            }
        }

        delegate DialogResult ShowThePrintDialog();
        public void PrintReport(ReportClass myReport, myTypes.PrintLayout printLayout, myTypes.PrintLayout defaultPrintLayout)
        {
            //To solve focus problem. See http://www.canbal.com/index.php?/Using-C-Sharp/resolved-printdialog-focus-problem.html
            myPrintDialog.UseEXDialog = false; 

            if (myPrintDialog.ShowDialog() != DialogResult.OK) return;
            myReport.PrintOptions.PrinterName = myPrintDialog.PrinterSettings.PrinterName;
            reportLibs.SetLayout(myReport, printLayout, defaultPrintLayout);
            myReport.PrintToPrinter(myPrintDialog.PrinterSettings.Copies, myPrintDialog.PrinterSettings.Collate,
                                    myPrintDialog.PrinterSettings.FromPage, myPrintDialog.PrinterSettings.ToPage);
            if (this.myOnPrinted != null) myOnPrinted(this);
        }


        public override void PrintReport()
        {
            if (myPrintDialog.ShowDialog() != DialogResult.OK) return;
            ReportClass myReport = (ReportClass)this.ReportSource;
            myReport.PrintToPrinter(myPrintDialog.PrinterSettings.Copies, myPrintDialog.PrinterSettings.Collate,
                                    myPrintDialog.PrinterSettings.FromPage, myPrintDialog.PrinterSettings.ToPage);
            if (this.myOnPrinted != null) myOnPrinted(this);
        }
        protected override void OnLayout(LayoutEventArgs e)
        {
            ApplyTheme();
            base.OnLayout(e);
        }
    }
}
